package xyz.example.securegeyser.listener;

import xyz.example.securegeyser.SecureGeyserBridge;
import xyz.example.securegeyser.AESCipher;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import net.kyori.adventure.text.Component;

import java.nio.charset.StandardCharsets;

public class IncomingSecureListener implements PluginMessageListener {
    private final SecureGeyserBridge plugin;

    public IncomingSecureListener(SecureGeyserBridge plugin) {
        this.plugin = plugin;
    }

    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if (!channel.equals(SecureGeyserBridge.CHANNEL)) return;
        try {
            byte[] plain = AESCipher.decrypt(plugin.getKey(), message);
            String text = new String(plain, StandardCharsets.UTF_8);
            plugin.getServer().broadcast(Component.text("[Secure] " + player.getName() + ": " + text));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
